package com.sylar.lelepub;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;

public class MainActivity extends TabActivity implements OnTabChangeListener, OnClickListener {
	private RadioGroup mainGroup;
	protected Resources mRes;
	private TabHost mTabHost;
	private RadioButton radio_near;
	private RadioButton radio_search;
	private RadioButton radio_order;
	private RadioButton radio_setting;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_main);

		mRes = getResources();

		mTabHost = (TabHost) findViewById(android.R.id.tabhost);

		mTabHost.setOnTabChangedListener(this);

		mTabHost.addTab(mTabHost.newTabSpec("RADIO_NEAR").setIndicator("radio_near").setContent(new Intent(this, TabNearActivity.class)));
		mTabHost.addTab(mTabHost.newTabSpec("RADIO_SEARCH").setIndicator("radio_search").setContent(new Intent(this, TabSearchActivity.class)));
		mTabHost.addTab(mTabHost.newTabSpec("RADIO_ORDER").setIndicator("radio_order").setContent(new Intent(this, TabOrderActivity.class)));
		mTabHost.addTab(mTabHost.newTabSpec("RADIO_SETTING").setIndicator("radio_setting").setContent(new Intent(this, TabSettingActivity.class)));

		radio_near = (RadioButton) findViewById(R.id.radio_near);
		radio_search = (RadioButton) findViewById(R.id.radio_search);
		radio_order = (RadioButton) findViewById(R.id.radio_order);
		radio_setting = (RadioButton) findViewById(R.id.radio_setting);
		radio_near.setOnClickListener(this);
		radio_search.setOnClickListener(this);
		radio_order.setOnClickListener(this);
		radio_setting.setOnClickListener(this);

		radio_near.performClick();
		
//		mainGroup = (RadioGroup) this.findViewById(R.id.main_radio);
//		mainGroup.check(R.id.radio_near);
//		mainGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//			public void onCheckedChanged(RadioGroup arg0, int rid) {
//				switch (rid) {
//				case R.id.radio_near:
//					mTabHost.setCurrentTabByTag("RADIO_NEAR");
//					break;
//				case R.id.radio_search:
//					mTabHost.setCurrentTabByTag("RADIO_SEARCH");
//					break;
//				case R.id.radio_order:
//					mTabHost.setCurrentTabByTag("RADIO_ORDER");
//					break;
//				case R.id.radio_setting:
//					mTabHost.setCurrentTabByTag("RADIO_SETTING");
//					break;
//
//				}
//			}
//		});

	}

	@Override
	public void onTabChanged(String tabId) {
		Activity activity = getLocalActivityManager().getActivity(tabId);
		if (activity != null) {
			activity.onWindowFocusChanged(true);
		}
		mTabHost.setCurrentTabByTag(tabId);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.radio_near:
			radio_near.setChecked(true);
			radio_search.setChecked(false);
			radio_order.setChecked(false);
			radio_setting.setChecked(false);
			mTabHost.setCurrentTabByTag("RADIO_NEAR");
			break;
		case R.id.radio_search:
			radio_near.setChecked(false);
			radio_search.setChecked(true);
			radio_order.setChecked(false);
			radio_setting.setChecked(false);
			mTabHost.setCurrentTabByTag("RADIO_SEARCH");
			break;
		case R.id.radio_order:
			radio_near.setChecked(false);
			radio_search.setChecked(false);
			radio_order.setChecked(true);
			radio_setting.setChecked(false);
			mTabHost.setCurrentTabByTag("RADIO_ORDER");
			break;
		case R.id.radio_setting:
			radio_near.setChecked(false);
			radio_search.setChecked(false);
			radio_order.setChecked(false);
			radio_setting.setChecked(true);
			mTabHost.setCurrentTabByTag("RADIO_SETTING");
			break;

		}
	}

	// @Override
	// public boolean dispatchKeyEvent(KeyEvent event) {
	// if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
	// if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount()
	// == 0) {
	// // 退出?
	// Toast.makeText(getApplicationContext(), "退出", 0).show();
	// finish();
	// }
	// return true;
	// } else {
	// return super.dispatchKeyEvent(event);
	// }
	//
	// }
}
